// eslint-disable-next-line prettier/prettier
import { defineConfig, loadEnv, ConfigEnv } from "vite";
import path from "path";
import createVitePlugins from "./vite/plugins";

// https://vitejs.dev/config/
export default defineConfig(({ mode, command }: ConfigEnv) => {
    const env = loadEnv(mode, process.cwd());
    const { VITE_APP_ENV } = env;
    return {
        // 部署生产环境和开发环境下的URL。
        // 默认情况下，vite 会假设你的应用是被部署在一个域名的根路径上
        base: VITE_APP_ENV === "production" ? "/" : "/",
        plugins: createVitePlugins(env, command === "build"),
        resolve: {
            // https://cn.vitejs.dev/config/#resolve-alias
            alias: {
                // 设置路径
                "~": path.resolve(__dirname, "./"),
                // 设置别名
                "@": path.resolve(__dirname, "./src"),
            },
            // https://cn.vitejs.dev/config/#resolve-extensions
            extensions: [".ts", ".js", "tsx", ".jsx", ".json", ".vue"],
        },
        // vite 相关配置
        server: {
            port: 80,
            host: true,
            open: true,
            proxy: {
                // https://cn.vitejs.dev/config/#server-proxy
                "/dev-api": {
                    target: "http://localhost:8080",
                    changeOrigin: true,
                    rewrite: (p) => p.replace(/^\/dev-api/, ""),
                },
            },
        },
        build: {
            // 混淆器设置
            minify: "terser",
            // 不生成source map文件，默认false
            sourcemap: true,
            // 指定输出路径（相对于项目根目录)，默认dist
            outDir: "dist",
            // 指定生成静态资源的存放路径，默认assets
            assetsDir: "assets",
            // chunk大小警告限制，默认500kbs
            chunkSizeWarningLimit: 1500,
            // 是否禁用css拆分(默认true)，设置false时所有CSS将被提取到一个CSS文件中
            cssCodeSplit: true,
            // 简要配置
            terserOptions: {
                compress: {
                    // 移除console
                    drop_console: true,
                    // 移除debugger
                    drop_debugger: true,
                },
                // 保留类名
                // keep_classnames: true,
                format: {
                    // 移除所有的注释
                    comments: false,
                },
            },
            // js、css等文件打包到不同文件夹
            /* rollupOptions: {
          output: {
              chunkFileNames: "assets/js/[name]-[hash].js",
              entryFileNames: "assets/js/[name]-[hash].js",
              assetFileNames: "assets/[ext]/[name]-[hash].[ext]"
          }
      } */
        },
        css: {
            devSourcemap: VITE_APP_ENV === "development" ? true : false,
            postcss: {
                plugins: [
                    {
                        postcssPlugin: "internal:charset-removal",
                        AtRule: {
                            charset: (atRule) => {
                                if (atRule.name === "charset") {
                                    atRule.remove();
                                }
                            },
                        },
                    },
                ],
            },
        },
    };
});
